<?php


namespace App\Http\Controllers;


use App\Models\User;

class ProfileController extends Controller
{
    /*
     * @api get /admin-api/a/users/profile 用户详情
     * @version 0.0.1
     * @group admin
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *
     * }
     * }
     */
    public function show()
    {
        /** @var User $userObj */
        $userObj = User::find($this->userId);
        $userInfo = [
            'id' =>  $userObj->getId(),
            'name' => $userObj->getName(),
            'mobile' => $userObj->getMobile()
        ];
        return $this->sendSuccessJson($userInfo);
    }

    /*
     * @api put /admin-api/a/users/profile 更新用户
     * @version 0.0.1
     * @group admin
     *
     * @param body string name 姓名
     * @param body string mobile 手机号
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function update()
    {
        $this->validate($this->request, [
            'name' => 'required|string',
            'mobile' => 'required|regex:/^1[345789]\d{9}$/',
        ]);
        $name = $this->request->input('name');
        $mobile = $this->request->input('mobile');
        /** @var User $userObj */
        $userObj = User::find($this->userId);
        $userObj->setName($name);
        $userObj->setMobile($mobile);
        $userObj->save();
        return $this->sendSuccessJson();
    }

    /*
     * @api put /admin-api/a/users/password 修改密码
     * @version 0.0.1
     * @group admin
     *
     * @param body string password 密码
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function updatePassword()
    {
        $this->validate($this->request, [
            'password' => 'required|alpha_dash|between:6,32',
        ]);
        $password = $this->request->input('password');
        /** @var User $userObj */
        $userObj = User::find($this->userId);
        $userObj->setPassword($password);
        $userObj->save();
        return $this->sendSuccessJson();
    }
}